<?php
class Category extends CI_Controller {

	
     function __construct() 
     {
     	
        parent::__construct();
        $this->load->database();
        $this->load->model('category_db');
        $this->load->helper('form');
        if($this->session->userdata('level') < 3 )
        {
        	redirect('http://google.be','refresh');
        }
     }
     
     function index()
     {
     	$data['page_title'] = 'Vos catégories';
		$data['folder'] = 'category';
		$data['main_content'] = 'view.php';
		$data['query'] = $this->category_db->getCategory_list();
		$this->load->view('admin/index',$data);
     }
     
     
     function add($action = NULL)
     {
     	if($action!='add_category')
     	{
	     	$data['page_title'] = 'Ajouter une catégorie';
			$data['folder'] = 'category';
			$data['main_content'] = 'add.php';
			$this->load->view('admin/index',$data);
     	}
     	else 
     	{
     		//Validation des champs du formulaire 
     		$this->load->library('form_validation');
			$this->form_validation->set_rules('name', 'name', 'required|min_length[5]|max_length[100]');

     		if ($this->form_validation->run() == FALSE)
				{
			     	$data['page_title'] = 'Ajouter une catégorie';
					$data['folder'] = 'category';
					$data['main_content'] = 'add.php';
					$this->load->view('admin/index',$data);
				}
			else
				{
		     		$name= $_POST['name'];
		     		$description = $_POST['description'];
		     		$this->category_db->addCategory($name,$description);
		     		redirect('/admin/category','refresh');
				}
     	}
     }
     
     function del($category_id)
     {
     	
     	if($this->category_db->deletteCategory($category_id)==TRUE)
     	{
     		$data['statut'] = "supprimée";
	     	$data['folder'] = 'category';
			$data['main_content'] = 'redirect.php';
			$this->load->view('admin/index',$data);
     	}
     }
     
}